<?php
namespace app\Admin\controller;
use think\Db;
use think\Validate;
use app\Admin\model\Tages as TagesModel;
use app\admin\controller\Base;
class Tages extends Base{
	public function index(){
		return $this->fetch();
	}

	public function lists(){
		$list=TagesModel::paginate(3);
		$this->assign('list',$list);
		return $this->fetch('list');
	}



	public function add(){
		if (request()->isPost()) {
			$data=[
				'tagname'=>input('tagname')
			];
			$validate= \think\Loader::validate('Tages');
			// var_dump($validate);exit();
			// if (!$validate->scene('add')->check($data)) {
			// 	return $this->error($validate->getError());
			// }
			if (Db::name('tages')->insert($data)) {
				return $this->success('添加成功！','lists');
			}else{
				return $this->error('添加失败！');
			}
			return;
		}
		return $this->fetch('add');
	}

	public function edit(){
		$id=input('id');
		$res=db('tages')->find($id);
		if (request()->isPost()) {
			$data=[
				'id'=>input('id'),
				'tagname'=>input('tagname'),
			];
			//数据的校验 
			// $validate= \think\Loader::validate('Tages');
			// if (!$validate->check($data)) {
			// 	return $this->error($validate->getError());
			// }

			$res=db('tages')->update($data);
			if ($res) {
				return $this->success('修改成功！','lists');
			}else{
				return $this->success('修改失败！');
			}
		}
		$this->assign('tags',$res);
		return $this->fetch('edits');
	}

	public function delete(){
		$id=input('id');
		$res=db('tages')->delete($id);
		if ($res) {
			$this->success("删除成功！",'lists');
		}else{
			$this->success("删除失败！",'');
		}
	}

}
?>